﻿using System.Web.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SubSonic.Security.Data;

[TestClass()]
public class MembershipProviderTests
{

    private TestContext testContextInstance;

    //CHANGE FOR DIFFERENT TEST RUNS
    private string username = "jblow";
    private string password = "helloworld";
    private string email = "hi@helloworld.com";
    private string question = "hi";
    private string answer = "hi";

    private string email2 = "zowens@ee.net";
    private string password2 = "thehhfdfesa";
    private string question2 = "blhahh";
    private string answer2 = "hgre";


    public TestContext TestContext
    {
        get { return testContextInstance; }
        set { testContextInstance = value; }
    }
    #region "Init and Cleanup"
    [TestInitialize()]
    public void CreateUser()
    {
        User.Delete(User.Columns.Username, username);
        System.Web.Security.MembershipCreateStatus mcs;
        MembershipUser mu = Membership.CreateUser(username, password, email, question, answer, true, out mcs);
    }

    [TestCleanup()]
    public void DeleteUser()
    {
        Membership.DeleteUser(username, true);
    }
    #endregion


    [TestMethod()]
    public void UpdateUser()
    {
        MembershipUser mu = Membership.GetUser(username);
        mu.Email = email2;

        Membership.UpdateUser(mu);


        User u = new User(User.Columns.Username, username);

        Assert.AreEqual(email2, u.Email);

        u.Email = email;
        u.IsNew = false;
        u.Save();
    }

    [TestMethod()]
    public void ChangePassword()
    {
        MembershipUser mu = Membership.GetUser(username);
        mu.ChangePassword(password, password2);

        User u = new User(User.Columns.Username, username);

        Assert.AreEqual(u.Password, password2);

        u.Password = password;
        u.IsNew = false;
        u.Save();
    }

    [TestMethod()]
    public void UnlockUser()
    {
        MembershipUser mu = Membership.GetUser(username);

        User u = new User(User.Columns.Username, username);
        u.IsValid = false;
        u.IsNew = false;
        u.Save();

        mu.UnlockUser();

        User u2 = new User(User.Columns.Username, username);

        Assert.AreEqual<bool>(u2.IsValid, true);

        u2.IsValid = true;
        u2.IsNew = false;
        u2.Save();


    }

    [TestMethod()]
    public void GetUserByEmail()
    {
        string un = Membership.GetUserNameByEmail(email);
        Assert.AreEqual(un, username);
    }

    [TestMethod()]
    public void ProviderConfiguration()
    {
        Assert.AreEqual(Membership.Provider.Name, "SubSonicMembershipProvider", true, "Provider Name isn't correct");
        Assert.AreEqual(Membership.ApplicationName, "TestSite", true, "AppName isn't correct");
        Assert.AreEqual(Membership.RequiresQuestionAndAnswer, true, "Requires Q&A isn't correct");
        Assert.AreEqual(Membership.EnablePasswordReset, true, "Enable pass reset isn't correct");
        Assert.AreEqual(Membership.EnablePasswordRetrieval, false, "Enable pass retrieval isn't correct");
        Assert.AreEqual(Membership.MaxInvalidPasswordAttempts, 10, "Max pass attempts isn't correct");
        Assert.AreEqual(Membership.MinRequiredNonAlphanumericCharacters, 0, "Min non-alpha isn't correct");
        Assert.AreEqual(Membership.MinRequiredPasswordLength, 5, "Min requires pass length isn't correct");
        Assert.AreEqual(Membership.PasswordAttemptWindow, 10, "Pass attempt window isn't correct");
        Assert.AreEqual(Membership.PasswordStrengthRegularExpression, string.Empty, false, "Password strength regex isn't correct");
        //Assert.AreEqual(Membership.UserIsOnlineTimeWindow, 4)
    }

    [TestMethod()]
    public void ChangePasswordQuestionAndAnswer()
    {
        MembershipUser mu = Membership.GetUser(username);
        mu.ChangePasswordQuestionAndAnswer(password, question2, answer2);

        User u = new User(User.Columns.Username, username);

        Assert.AreEqual(u.SecurityQuestion, question2);
        Assert.AreEqual(u.SecurityAnswer, answer2);

        u.SecurityQuestion = question;
        u.SecurityAnswer = answer;
        u.IsNew = false;
        u.Save();
    }

    [TestMethod()]
    public void GetAllUsers()
    {
        MembershipUserCollection col = Membership.GetAllUsers();

        Assert.AreEqual(col.Count, 1);
        Assert.AreEqual(col[username].Email, email);
    }

}